package com.example.blog.service.impl;

import com.example.blog.dao.PictureMapper;
import com.example.blog.pojo.Picture;
import com.example.blog.service.PictureService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @version 1.0
 * @date: 2020/9/5
 * @author: 东北宋小宝
 * @Description:
 */
@Service
public class PictureServiceImpl implements PictureService {
    @Resource
    private PictureMapper pictureMapper;
    @Override
    public void savePicture(Picture picture) {
        if (picture.getId() == null) {
            pictureMapper.insertPicture(picture);
        } else {
            pictureMapper.updatePicture(picture);
        }
    }

    @Override
    public Picture getPictureById(Long id) {
        Picture picture = pictureMapper.getPictureById(id);
        return picture;
    }

    /**
     * @param page
     * @param limit
     * @return
     */
    @Override
    public PageInfo<Picture> getAllPicture(Integer page, Integer limit) {
        if (page == null){ page= 1; }
        PageHelper.startPage(page,limit,true);
        List<Picture> list = pictureMapper.getAllPicture(null);
        PageInfo<Picture> pageInfo = new PageInfo<>(list);
        return pageInfo;
    }

    /**
     * 删除picture
     *
     * @param id picture id
     */
    @Override
    public void delPicture(Long id) {
        pictureMapper.deletPicture(id);
    }

    /***
     * 获取所有picture 不分页
     * @return
     */
    @Override
    public List<Picture> getAllPicture() {
        return pictureMapper.getAllPicture(null);
    }
}
